import org.zjx.task.*;
import org.zjx.context.ChainContext;
import org.zjx.task.core.ITask;
import org.zjx.task.core.TaskScheduler;

import java.util.Arrays;
import java.util.List;

public class ChainDemo {

    public static void main(String[] args) throws InterruptedException {
        TaskScheduler scheduler = new TaskScheduler(4,null);

        // 创建可重试任务（最大重试3次）
        RetryTask retryTask = new RetryTask("retryTask") {
            @Override
            public void execute(ChainContext context,TaskScheduler scheduler) throws Exception {
                Thread.sleep(1000);
                if ((context.getCurrentChain().getRestartCount()) < 2) { // 前两次失败
                    throw new Exception("Retry test failure");
                }
                System.out.println("RetryTask succeeded on retry " + context.getCurrentChain().getRestartCount());
            }
        };
        retryTask.setMaxRetries(10);
        retryTask.setFailureStrategy(ITask.FailureStrategy.RESTART);

        RetryTask retryTask2 = new RetryTask("retryTask2") {
            @Override
            public void execute(ChainContext context,TaskScheduler scheduler) throws Exception {
                Thread.sleep(1000);
                System.out.println("RetryTask2 start" );
                if (getRetryCount() < 8) {
                    throw new Exception("Retry2 test failure");
                }
                System.out.println("RetryTask2 end");
            }
        };
        retryTask2.setMaxRetries(10);
        retryTask2.setFailureStrategy(ITask.FailureStrategy.STOP);

        // 创建任务链
        List<ITask> tasks = Arrays.asList(
                new DemoTask("task1", false, ITask.FailureStrategy.CONTINUE),
                retryTask,
                retryTask2,
                new DemoTask("task3", true, ITask.FailureStrategy.STOP)
        );
        AbstractChain chain = new AbstractChain("demoChain") {};
        chain.setTasks(tasks);
        chain.setMaxRestart(3); // 链最大重启3次

        scheduler.addChain(chain);

//        try {
//            Thread.sleep(1000);
//            scheduler.executeTask(retryTask2,null);
//        } catch (TaskExecutionException e) {
//            throw new RuntimeException(e);
//        }


        // 等待执行
        Thread.sleep(12000);
        scheduler.shutdown();
    }

    static class DemoTask extends AbstractTask {
        private boolean shouldFail;

        public DemoTask(String id, boolean shouldFail, ITask.FailureStrategy strategy) {
            super(id);
            this.shouldFail = shouldFail;
            this.failureStrategy = strategy;
        }

        @Override
        public void execute(ChainContext context,TaskScheduler scheduler) throws Exception {
            if (shouldFail) {
                shouldFail = false;
                throw new Exception("DemoTask failed");
            }
            System.out.println(getTaskId() + " executed successfully.");
        }

        @Override
        public FailureStrategy getFailureStrategy() {
            return FailureStrategy.FALLBACK;
        }
    }
}